package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.domain.Project;
import com.woniuxy.dto.DtoJSONResult;
import com.woniuxy.dto.DtoJSONResultCode;
import com.woniuxy.dto.DtoProject;
import com.woniuxy.parameter.ProjectParam;
import com.woniuxy.service.ProjectService;
import com.woniuxy.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProjectController
 * @Description 项目增删
 * @Author 李磊
 * @Version 1.0
 **/
@RestController
@RequestMapping("/project")
@CrossOrigin
@Api(tags = "项目增删")
public class ProjectController {
    /*项目业务层注入*/
    @Autowired
    private ProjectService projectService;

    /**
    *@Author 李磊
    *@Description
    *@Param [projectParam]
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @PostMapping
    @ApiOperation("新增项目")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult insert(ProjectParam projectParam){
        System.out.println(projectParam);
        Project project = new Project();
        BeanUtils.copyProperties(projectParam,project);
        projectService.save(project);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);
    }

    /**
    *@Author 李磊
    *@Description 查询所有项目
    *@Param []
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @GetMapping
    @ApiOperation("查询所有项目")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult query(){

        List<Project> list = projectService.list();

        List<Object> properties = BeanCopyUtil.copyListProperties(list, DtoProject::new);

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,properties);
    }
    /**
    *@Author 李磊
    *@Description 删除项目
    *@Param [id]
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @DeleteMapping
    @ApiOperation("删除项目")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult delete(int id){

        projectService.removeById(id);

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);
    }
    /**
    *@Author 李磊
    *@Description 修改项目
    *@Param [projectParam]
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @PutMapping
    @ApiOperation("修改项目")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult update(ProjectParam projectParam){

        Project project = new Project();
        BeanUtils.copyProperties(projectParam,project);
        projectService.saveOrUpdate(project);

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);

    }
    /**
    *@Author 李磊
    *@Description 根据项目名查询项目信息
    *@Param [name]
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @GetMapping("/queryByName")
    @ApiOperation("根据项目名查询项目信息")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryByName(String projectName){
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.like("project_name",projectName);
        List list = projectService.list(wrapper);
        List list1 = BeanCopyUtil.copyListProperties(list, DtoProject::new);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,list1);
    }

}
